﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using HelpLibrary.MyLog;
using HelpLibrary.SQlite;
using System.Linq;

namespace HelpLibrary
{
    public class DbHelp

    {
        public static void Add(string itemvalue)
        {
            MySQliteContext db = new MySQliteContext();
            using (db)
            {
                var t = db.gr.Where(x => x.CreateTime == DateTime.Today).FirstOrDefault();
                float tempfloat;
                if (t == null)
                {
                    t = new Gr();
                    t.CreateTime = DateTime.Today;
                }

                switch (DateTime.Now.Hour)
                {
                    case 0:
                        float.TryParse(itemvalue, out tempfloat);
                        t.a = tempfloat;
                        break;
                    case 8:
                        float.TryParse(itemvalue, out tempfloat);
                        t.b = tempfloat;
                        break;
                    case 12:
                        float.TryParse(itemvalue, out tempfloat);
                        t.c = tempfloat;
                        break;
                    case 16:
                        float.TryParse(itemvalue, out tempfloat);
                        t.d = tempfloat;
                        break;
                    case 18:
                        float.TryParse(itemvalue, out tempfloat);
                        t.e= tempfloat;
                        break;
                    case 22:
                        float.TryParse(itemvalue, out tempfloat);
                        t.f = tempfloat;
                        break;
                    default:
                        t.e = 222.0f;
                        break;

                }
                 db.gr.AddOrUpdate(t);
                //Entry(t).State = EntityState.Added;
                int ii = db.SaveChanges();
                
                if (ii>0)
                {
                    MyLogHelper.LogHelper.WriteInfo($"Add {itemvalue} success! at {DateTime.Now}");
                }
                else
                {
                    var g = t;
                    MyLogHelper.LogHelper.WriteInfo($"Add {itemvalue} Fail! at {DateTime.Now}");
                    MyLogHelper.LogHelper.WriteError($"Add {itemvalue} Fail! at {DateTime.Now}");
                }

                
                
            }
            
        }
        /// <summary>
        /// 生成班次信息
        /// </summary>
        /// <param name="firstWorkShift">班次</param>
        /// <param name="dt">时间</param>
        /// <returns></returns>
        public static List<Gr> SortShift(WorkShift firstWorkShift, DateTime dt)
        {
            MySQliteContext db = new MySQliteContext();
            using (db)
            {
                var s=db.gr.ToList();
               // s.First(x=>x.CreateTime==dt).YeBan=firstWorkShift;
                foreach (var gr in s)
                {
                    int Dayspan = ((gr.CreateTime - dt).Days)%8;
                    if (Dayspan<0)
                    {
                        Dayspan = 8 + Dayspan;
                    }
                    if (Dayspan==0||Dayspan==1)
                    {
                        gr.YeBan = firstWorkShift;
                        gr.Baiban = WsCirle(firstWorkShift, 1);
                        gr.Zhongban = WsCirle(firstWorkShift, 2);
                    }

                    if (Dayspan == 2||Dayspan==3)
                    {
                        gr.YeBan = WsCirle(firstWorkShift,3);
                        gr.Baiban = WsCirle(gr.YeBan, 1);
                        gr.Zhongban = WsCirle(gr.Baiban, 1);
                    }
                    if (Dayspan == 4 || Dayspan == 5)
                    {
                        gr.YeBan = WsCirle(firstWorkShift, 2);
                        gr.Baiban = WsCirle(gr.YeBan, 1);
                        gr.Zhongban = WsCirle(gr.Baiban, 1);
                    }
                    if (Dayspan == 6 || Dayspan == 7)
                    {
                        gr.YeBan = WsCirle(firstWorkShift, 1);
                        gr.Baiban = WsCirle(gr.YeBan, 1);
                        gr.Zhongban = WsCirle(gr.Baiban, 1);
                    }


                }

                return db.gr.ToList();
            }

        }

        private static WorkShift WsCirle(WorkShift firstWorkShift, int num)
        {
            var i = ( (int) firstWorkShift + num) % 4;
           return (WorkShift) Enum.Parse(typeof(WorkShift), i.ToString());
        }
    }
}
